﻿<%@ Control Language="C#"
            AutoEventWireup="true"
            CodeFile="ContactSelector.ascx.cs"
            Inherits="UserControls_ContactSelector" %>

<asp:Panel ID="ControlContainer" runat="server" CssClass="field">
    <div class="label">
        <asp:Label ID="ControlLabelText" runat="server" Text="Contactos"></asp:Label>
        <asp:Label ID="MessageLabel" runat="server" CssClass="mensajes"></asp:Label>
    </div>
    <div>
        <asp:DropDownList ID="TipoContactoDropDownList" runat="server"
            DataTextField="Descripcion"
            DataValueField="TipoContactoId"
            DataSourceID="TipoContactoDataSource" 
            ondatabound="TipoContactoDropDownList_DataBound">
        </asp:DropDownList>
        <asp:ObjectDataSource ID="TipoContactoDataSource" runat="server"
            TypeName="Iudicium.Parametros.CRN.TipoContactoCRN"
            SelectMethod="GetTipoContactoBySearch" 
            onselected="TipoContactoDataSource_Selected">
            <SelectParameters>
                <asp:Parameter Name="parametro" Type="String" DefaultValue="" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:TextBox ID="ContactDescriptionTextBox" runat="server"
            AutoPostBack="false"
            Enabled="false"
            CssClass="normalField">
        </asp:TextBox>
        <asp:LinkButton ID="AddContactCommand" runat="server" Text="Agregar"></asp:LinkButton> 
    </div>
</asp:Panel>
<div class="field">
    <div id="ContactContainer" runat="server" class="field">
        <asp:Repeater ID="ContactRepetater" runat="server">
            <HeaderTemplate>
                <table width="450" border="0" cellpadding="0" cellspacing="0" class="dataTable">
                    <col width="150" />
                    <col width="220" />
                    <col width="80" />
                    <thead>
                        <tr class="header">
                            <th>Tipo Contacto</th>
                            <th>Descipcion</th>
                            <th>&nbsp;</th>
                        </tr>
                    </thead>
                    <tbody>                        
            </HeaderTemplate>
            <ItemTemplate>
                        <tr class="item0">
                            <td>
                                <asp:Literal ID="TipoContactoLiteral" runat="server"
                                    Text="<%# Bind('DescripcionTipoContacto') %>">
                                </asp:Literal>
                            </td>
                            <td>
                                <asp:Literal ID="DescripcionContacto" runat="server"
                                    Text="<%# Bind('Descripcion') %>">
                                </asp:Literal>
                            </td>
                            <td>
                                <asp:HyperLink ID="cmd" NavigateUrl="#" runat="server" CssClass="<%# GetCssClass() %>">Quitar</asp:HyperLink>
                                <asp:HiddenField ID="IdHiddenFiedl" runat="server" Value="<%# Bind('ContactoId') %>" />
                            </td>
                        </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                        <tr class="item1">
                            <td>
                                <asp:Literal ID="TipoContactoLiteral" runat="server"
                                    Text="<%# Bind('DescripcionTipoContacto') %>">
                                </asp:Literal>
                            </td>
                            <td>
                                <asp:Literal ID="DescripcionContacto" runat="server"
                                    Text="<%# Bind('Descripcion') %>">
                                </asp:Literal>
                            </td>
                            <td>
                                <asp:HyperLink ID="cmd" NavigateUrl="#" runat="server" CssClass="<%# GetCssClass() %>">Quitar</asp:HyperLink>
                                <asp:HiddenField ID="IdHiddenFiedl" runat="server" Value="<%# Bind('ContactoId') %>" />
                            </td>
                        </tr>
            </AlternatingItemTemplate>
            <FooterTemplate>
                        <asp:Literal ID="EmptyTemplateLiteral" runat="server" Visible="<%# IsRepeaterVisible() %>"
                            Text='<tr class="item0 emptyDataSet"><td colspan="3" align="center">No hay contactos agregados</td></tr>'>
                        </asp:Literal>
                    </tbody>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
</div>

<asp:HiddenField ID="PropietarioIdHiddenField" runat="server" Value="0" />
<asp:HiddenField ID="TipoPropietarioHiddenField" runat="server" Value="Persona" />
<asp:HiddenField ID="InsertedContactHiddenField" runat="server" Value="[]" />
<asp:HiddenField ID="DeletedContactHiddenField" runat="server" Value="[]" />

<script type="text/javascript">

    var insertedContact = new Array();
    var deletedContact = new Array();
    
    $(document).ready(function(){
        
        $("#<%= AddContactCommand.ClientID %>").click(function(){
            return addContact();
        });
        
        $("#<%= TipoContactoDropDownList.ClientID %>").change(function(){
            var val = $("#<%= TipoContactoDropDownList.ClientID %>").val();
            if(val == "" || val == "0"){
                $("#<%= ContactDescriptionTextBox.ClientID %>").val("");
                $("#<%= ContactDescriptionTextBox.ClientID %>").attr("disabled","true");
                return;
            }
            $("#<%= ContactDescriptionTextBox.ClientID %>").removeAttr("disabled");
            $("#<%= ContactDescriptionTextBox.ClientID %>").focus();
        });
        
        $("#<%= ContactDescriptionTextBox.ClientID %>").keypress(function(event) {
            if (event.keyCode == 13) {
                addContact();
                return false;
            }
            return true;
        });
        
        $(".<%= GetCssClass() %>").live("click",function(){
            var id = $(this).parent().children("input[type='hidden']").val();
            if(id > 0)
                deletedContact[deletedContact.length] = parseInt(id);        
            $(this).parent().parent().remove();
            updateCssClass();
            if($("#<%= ContactContainer.ClientID %> > table > tbody > tr").length == 0)
                $("#<%= ContactContainer.ClientID %> > table > tbody").html(getEmptyData());
            updateHiddenFields();
            return false;
        });
    });
    
    function getEmptyData(){
        return '<tr class="item0 emptyDataSet"><td colspan="3" align="center">No hay contactos agregados</td></tr>';
    }
    
    function addContact(){
        var contactTypeId = $("#<%= TipoContactoDropDownList.ClientID %>").val();
        if(contactTypeId == 0 || contactTypeId == ""){
            $("#<%= MessageLabel.ClientID %>").html("Seleccione el Tipo de Contacto");
            return false;
        }
        $("#<%= MessageLabel.ClientID %>").html("");
        
        var description = superTrim($("#<%= ContactDescriptionTextBox.ClientID %>").val());
        if(description == ""){
            $("#<%= MessageLabel.ClientID %>").html("Ingrese la Descripcion del contacto");
            return false;
        }
        $("#<%= MessageLabel.ClientID %>").html("");
        
        var contactTypeDescription = $("#<%= TipoContactoDropDownList.ClientID %> option:selected").text();
        var obj = getJsonObject(description, contactTypeId, contactTypeDescription);
        insertedContact[insertedContact.length] = obj;
        
        $("#<%= ContactDescriptionTextBox.ClientID %>").val("");
        $("#<%= TipoContactoDropDownList.ClientID %>").val(0);
        $("#<%= ContactContainer.ClientID %> > table > tbody").append(getRow(0,description, contactTypeDescription));
        $("#<%= ContactDescriptionTextBox.ClientID %>").attr("disabled","true");
        $("#<%= TipoContactoDropDownList.ClientID %>").focus();
        updateHiddenFields();
        return false;
    }
    
    function updateCssClass(){
        var trs = $("#<%= ContactContainer.ClientID %> > table > tbody > tr");
        $(trs).removeClass("item0");
        $(trs).removeClass("item1");
        for(var i = 0 ; i < trs.length; i++){
            $(trs[i]).addClass("item" + (i % 2));
        }
    }
    
    function getJsonObject(description, contactTypeId, contactTypeDescription){
        var obj = 
            { 
                ContactoId : 0,
                PropietarioId : <%= PropietarioId %>,
                TipoContactoId: contactTypeId,
                DescripcionTipoContacto : contactTypeDescription,
                Descripcion: description,
                TipoPropietarioContacto: '<%= TipoPropietario %>'
            }
        return obj;
    }
    
    function getRow(id,description, contactTypeDescription){
        var count = $("#<%= ContactContainer.ClientID %> > table > tbody > tr.emptyDataSet").length;
        if (count == 1) {
            count = 0;  
            $("#<%= ContactContainer.ClientID %> > table > tbody").html("");
        }else   
            count = $("#<%= ContactContainer.ClientID %> > table > tbody > tr").length;
        return '<tr class="item' + (count % 2) + '">' + 
                    '<td>' + contactTypeDescription + '</td>' + 
                    '<td>' + description + '</td>' +
                    '<td>' +
                        '<a href="#" class="<%= GetCssClass() %>">Quitar</a>' +
                        '<input type="hidden" value="' + id + '" />' +
                    '</td>' +
                '</tr>';
    }
    
    function updateHiddenFields(){
        $("#<%= InsertedContactHiddenField.ClientID %>").val(JSON.stringify(insertedContact));
        $("#<%= DeletedContactHiddenField.ClientID %>").val(JSON.stringify(deletedContact));
    }
    
    
</script>

